<template>
  <div class="clientType">
    <global-card>
      <template #leftTitle>
        <perButton label="新建规则" type="primary" icon="el-icon-plus" perms="PROTOCOL_ADD"
                   @click="newCompanyClick"></perButton>
        <el-button type="success" icon="el-icon-refresh" @click="refresh">刷新</el-button>
      </template>
      <template #content>
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          style="width: 100%"
          class="elTable"
        >
          <el-table-column type="index" label="序号" width="80" align="center"/>
          <el-table-column prop="beginOrderAmout" label="交易额开始区间" align="center"/>
          <el-table-column prop="endOrderAmout" label="交易额结束区间" align="center"/>
          <el-table-column prop="rate" label="提成比例(%)" align="center"/>
          <el-table-column prop="createByName" label="创建人" align="center"/>
          <el-table-column label="创建时间" align="center" prop="createTime" width="170">
            <template slot-scope="scope">
              {{ scope.row.createTime}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template #default="{ row }">
              <perButton label="编辑" type="warning" size="mini" perms="PROTOCOL_EDIT"
                         @click="handlerEdit(row)"></perButton>
              <perButton label="删除" type="danger" size="mini" perms="PROTOCOL_DEL"
                         @click="handlerDelete(row)"></perButton>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #footer>
        <pagination
          class="pagination"
          :total="count"
          :page.sync="pageIndex"
          :limit.sync="pageSize"
          @pagination="pagination"
        />
      </template>
    </global-card>

    <yc-dialog
      ref="ycDialog"
      :dialogTitle="dialogTitle"
      :centerTitle="dialogCenterTitle"
    >
      <template #content>
        <div class="dialog-content">
          <el-form
            :model="newModel"
            :rules="rules"
            ref="modelRef"
            label-width="130px"
          >
            <el-form-item label="交易额开始区间" prop="beginOrderAmout">
              <el-input-number v-model="newModel.beginOrderAmout" controls-position="right" :min="0" :max="1000000000"></el-input-number>
            </el-form-item>
            <el-form-item label="交易额结束区间" prop="endOrderAmout">
              <el-input-number v-model="newModel.endOrderAmout" controls-position="right" :min="0" :max="1000000000"></el-input-number>
            </el-form-item>
            <el-form-item label="提成比例(%)" prop="rate">
              <el-input-number v-model="newModel.rate" controls-position="right" :min="0" :max="100"></el-input-number>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="addOrUpdateLoading" @click="dialogSave">确定</el-button>
        <el-button @click="$refs.ycDialog.dialogVisible = false">取消
        </el-button>
      </template>
    </yc-dialog>

    <global-message
      v-if="isGlobalMessageShow"
      @receive="receive"
      :message="message"
    />
  </div>
</template>

<script>
  import ycDialog from "@/components/pageDialog"
  import perButton from "@/components/permissionButton"
  import {
    queryPageMajordomo,
    insertMajordomo,
    updateMajordomo,
    deleteMajordomo
  } from "@/api/basicSetupManagement/majordomoRoyalty"

  export default {
    components: {
      ycDialog,
      perButton
    },
    data() {
      return {
        tableData: [],
        pageIndex: 1,
        pageSize: 10,
        count: 0,
        searchForm: {
          agreementName: "",
          agreementType: ""
        },

        detailsDisabled: false,
        isGlobalMessageShow: false,

        dialogCenterTitle: "",
        dialogTitle: "",
        message: "",

        loading: false,
        newModel: {
          id: "",
          beginOrderAmout: "",
          endOrderAmout: "",
          rate: ""
        },
        rules: {
          beginOrderAmout: [
            { required: true, message: "请输入交易额开始区间", trigger: "blur" }
          ],
          endOrderAmout: [
            { required: true, message: "请输入交易额结束区间", trigger: "blur" }
          ],
          rate: [{ required: true, message: "请输入提成比例", trigger: "blur" }]
        },
        addOrUpdateLoading: false
      }
    },
    created() {
      this.getData()
    },
    methods: {
      async searchClick() {
        this.getData()
      },
      async getData() {
        const { agreementName, agreementType } = this.searchForm
        this.loading = true
        const data = await queryPageMajordomo({
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          agreementName: agreementName,
          agreementType: agreementType
        })
        this.tableData = data.object.data
        this.count = data.object.count
        this.loading = false
      },
      pagination(pageItem) {
        this.getData(pageItem.page, pageItem.limit)
      },


      handlerDelete({ id }) {
        this.$confirm("确定删除此数据吗", "删除操作", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error",
          center: true
        }).then(async () => {
          const { status } = await deleteMajordomo({
            id,
            isDeleted: 1
          })
          if (status == 200) {
            this.getData()
            this.isGlobalMessageShow = true
            this.message = "success"
          } else {
            this.message = "error"
          }
        })
      },

      // 新建规则
      newCompanyClick() {
        this.dialogCenterTitle = "新建规则"
        this.dialogTitle = "新建规则"

        Object.keys(this.newModel).forEach((item) => {
          this.newModel[item] = ""
        })

        this.detailsDisabled = false
        this.addOrUpdateLoading = false
        this.$refs.ycDialog.dialogVisible = true
        if (this.$refs["modelRef"]) {
          this.$refs["modelRef"].resetFields()
          this.$refs["modelRef"].clearValidate()
        }
      },
      handlerEdit(row) {
        this.dialogCenterTitle = "编辑规则"
        this.dialogTitle = "编辑规则"

        this.newModel = {
          id: row.id,
          beginOrderAmout: row.beginOrderAmout,
          endOrderAmout: row.endOrderAmout,
          rate: row.rate
        }

        this.addOrUpdateLoading = false
        this.detailsDisabled = false
        this.$refs.ycDialog.dialogVisible = true
        if (this.$refs["modelRef"]) {
          this.$refs["modelRef"].resetFields()
          this.$refs["modelRef"].clearValidate()
        }
      },

      async dialogSave() {
        this.$refs.modelRef.validate(async (v) => {
          if (v) {
            this.addOrUpdateLoading = true

            if (this.detailsDisabled) {
              // 详情
              this.$refs.ycDialog.dialogVisible = false
            } else if (this.newModel.id) {
              //  编辑
              const { status, message } = await updateMajordomo({
                ...this.newModel
              })
              if (status == 200) {
                this.$message({
                  message: message,
                  type: "success"
                })
                this.getData()
                this.$refs.ycDialog.dialogVisible = false
              }
            } else {
              const { status, message } = await insertMajordomo({ ...this.newModel })
              if (status == "200") {
                this.$message({
                  message: message,
                  type: "success"
                })
                this.getData()
                this.$refs.ycDialog.dialogVisible = false
              }
            }
            this.addOrUpdateLoading = false
          }
        })
      },

      receive() {
        this.isGlobalMessageShow = false
      },

      refresh() {
        this.searchForm = {
          agreementName: "",
          agreementType: ""
        }
        this.getData()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .clientType {
    .title-container {
      display: flex;
      justify-content: flex-end;
      margin-right: 20px;

      .left-box {
        display: flex;

        .formClass {
          /*display: flex;*/
          /*margin-right: 20px;*/

          .el-form-item {
            margin-top: 20px;
            margin-bottom: 0;

            .el-input {
              width: 216px;
            }

            .el-select {
              width: 171px;
              margin-left: 10px;
            }

            .el-date-editor {
              width: 336px;
              margin-left: 10px;
            }
          }
        }

        /*.el-button {*/
        /*  // width: 42px;*/
        /*  height: 36px;*/
        /*  margin-top: 20px;*/
        /*}*/
      }
    }
  }
</style>
